
#' Writes annotated genomic regions
#'
#' Creates a CSV file with genomic regions annoted as TSS-proximal and TSS-distal regions.
#' categorized by cell type
#'
#' @param countsPeaks output generated from getCounts()
#' @param con connection filepath
#'
#' @examples
#' \dontrun{
#' csvfile <- file.path(dir="yourfilepath", 'sampleinfo.csv')
#' sampleinfo <- loadCSVFile(csvfile)
#' samplePeaks <- loadBedFiles(sampleinfo)
#' consPeaks <- getConsensusPeaks(samplepeaks=samplePeaks,minreps=2)
#' TSSannot <- getTSS()
#' consPeaksAnnotated <- combineAnnotatePeaks(conspeaks = consPeaks,
#'                                           TSS = TSSannot,
#'                                           merge = TRUE,
#'                                           regionspecific = TRUE,
#'                                           distancefromTSSdist = 1500,
#'                                           distancefromTSSprox = 1000 )
#' con <- "annotatedRegions.csv"
#' writeConsensusRE(consPeaksAnnotated, con)
#' }
#' @export
writeConsensusRE <- function(countsPeaks, con) {

    annotatedRegions <- GenomicRanges::as.data.frame(
      countsPeaks$consPeaksAnnotated)
    readr::write_csv(annotatedRegions, con)
  }


#' Creates a custom track for visualization on genome browser
#'
#' Creates a color-coded BED file for visualization of peaks and their ALTRE
#' categories in a genome browser: red indicates increased log2fold change/low
#' p-value, blue indicates decreased lod2fold change/low p-value, and purple
#' indicates regions with little to no change and insignificant p-values.
#' Based on the log2fold change and p-value inputs, there is a possibility that
#' some regions will not fulfill any of the supplied criteria
#' ("in-between" shared and type-specific). They are colored grey.
#'
#' @param analysisresults output generated from categAltrePeaks()
#' @param con connection
#'
#' @examples
#' \dontrun{
#' csvfile <- file.path(dir="yourfilepath", 'sampleinfo.csv')
#' sampleinfo <- loadCSVFile(csvfile)
#' samplePeaks <- loadBedFiles(sampleinfo)
#' consPeaks <- getConsensusPeaks(samplepeaks=samplePeaks,minreps=2)
#' TSSannot <- getTSS()
#' consPeaksAnnotated <- combineAnnotatePeaks(conspeaks = consPeaks,
#'                                           TSS = TSSannot,
#'                                           merge = TRUE,
#'                                           regionspecific = TRUE,
#'                                           distancefromTSSdist = 1500,
#'                                           distancefromTSSprox = 1000)
#' #Need to run getcounts on all chromosomes
#' counts_consPeaks <- getcounts(annotpeaks = consPeaksAnnotated,
#'                              csvfile = csvfile,
#'                              reference = 'SAEC')
#' altre_peaks <- countanalysis(counts = counts_consPeaks,
#'                              pval = 0.01,
#'                              lfcvalue = 1)
#' categaltre_peaks <- categAltrePeaks(altre_peaks, lfctypespecific = 1.5,
#'	lfcshared = 1.2, pvaltypespecific = 0.01, pvalshared = 0.05)
#' writeBedFile(categaltre_peaks)
#' }
#' @export

writeBedFile <-
  function(analysisresults, con) {

    analysisresults <- analysisresults[[1]]
    if (is.data.frame(analysisresults) == FALSE) {
      stop("The input for the analysisresults arguement is not a dataframe!")
    }

    colors <- c("grey", "salmon", "green", "blue")

    mycol <- analysisresults$REaltrecateg
    mycol[which(mycol == "Ambiguous")] <-
      paste(as.numeric(grDevices::col2rgb(colors[1])),
            sep = ",",
            collapse = ",")
    mycol[which(mycol == "Shared")] <-
      paste(as.numeric(grDevices::col2rgb(colors[2])),
            sep = ",",
            collapse = ",")
    mycol[which(mycol == "Reference Specific")] <-
      paste(as.numeric(grDevices::col2rgb(colors[3])),
            sep = ",",
            collapse = ",")
    mycol[which(mycol == "Experiment Specific")] <-
      paste(as.numeric(grDevices::col2rgb(colors[4])),
            sep = ",",
            collapse = ",")

    bedfile <- data.frame(
      chrom = analysisresults$chr,
      chromStart  = analysisresults$start,
      chromEnd = analysisresults$stop,
      name = paste0("peak", 1:nrow(analysisresults)),
      score = "0",
      strand = "+",
      thickStart = ".",
      thickEnd = ".",
      itemRgb = mycol
    )

    header <-
      'track name=ALTREtrack description="ALTRE categories" visibility=2 itemRgb="On"'

    utils::write.table(
      header,
      file = con,
      row.names = FALSE,
      col.names = FALSE,
      quote = FALSE
    )

    utils::write.table(
      bedfile,
      file = con,
      row.names = FALSE,
      col.names = FALSE,
      append = TRUE,
      quote = FALSE,
      sep = "\t"
    )

  }

#' Writes a data table generated by comparePeaksAltre
#'
#' Creates a CSV file with the number of peaks called as experiment- or
#' reference-specific and shared using the quantitative/peak intensity or
#' peak/binary approach.
#'
#' @param comparePeaksOut output generated from comparePeaksAltre()
#' @param con connection filepath
#'
#' @examples
#' \dontrun{
#' csvfile <- file.path(dir="yourfilepath", 'sampleinfo.csv')
#' sampleinfo <- loadCSVFile(csvfile)
#' samplePeaks <- loadBedFiles(sampleinfo)
#' consPeaks <- getConsensusPeaks(samplepeaks=samplePeaks,minreps=2)
#' TSSannot <- getTSS()
#' consPeaksAnnotated <- combineAnnotatePeaks(conspeaks = consPeaks,
#'                                           TSS = TSSannot,
#'                                           merge = TRUE,
#'                                           regionspecific = TRUE,
#'                                           distancefromTSSdist = 1500,
#'                                           distancefromTSSprox = 1000)
#' #Need to run getcounts on all chromosomes
#' counts_consPeaks <- getcounts(annotpeaks = consPeaksAnnotated,
#'                              csvfile = csvfile,
#'                              reference = 'SAEC')
#'                              #' altre_peaks <- countanalysis(counts = counts_consPeaks,
#'                              pval = 0.01,
#'                              lfcvalue = 1)
#' categaltre_peaks <- categAltrePeaks(altre_peaks,
#'                                     lfctypespecific = 1.5,
#'                                     lfcshared = 1.2,
#'                                     pvaltypespecific = 0.01,
#'                                     pvalshared = 0.05)
#' comparePeaksOut <- comparePeaksAltre(categaltre_peaks, reference= 'SAEC')
#' con <- "datatableRE.csv"
#' writeCompareRE(comparePeaksOut, con)
#'
#' }
#' @export

writeCompareRE <-
  function(comparePeaksOut, con) {
    fileOut <- tibble::rownames_to_column(as.data.frame(comparePeaksOut[[1]]))
    readr::write_csv(fileOut, con)
  }


#### Writes a data generated by pathenrich
####
#### Creates  three CSV files containing the results of the pathway enrichment analysis for
#### experiment-specific, reference-specific, and shared REs.
####
#### @param pathenrichOut output generated from pathenrich()
#### @param con connection filepath
####
#### @examples
#### \dontrun{
#### csvfile <- file.path(dir="yourfilepath", 'sampleinfo.csv')
#### sampleinfo <- loadCSVFile(csvfile)
#### samplePeaks <- loadBedFiles(sampleinfo)
#### consPeaks <- getConsensusPeaks(samplepeaks=samplePeaks,minreps=2)
#### TSSannot <- getTSS()
#### consPeaksAnnotated <- combineAnnotatePeaks(conspeaks = consPeaks,
####                                           TSS = TSSannot,
####                                           merge = TRUE,
####                                           regionspecific = TRUE,
####                                           distancefromTSSdist = 1500,
####                                           distancefromTSSprox = 1000)
#### #Need to run getcounts on all chromosomes
#### counts_consPeaks <- getcounts(annotpeaks = consPeaksAnnotated,
####                              csvfile = csvfile,
####                              reference = 'SAEC')
####                              #' altre_peaks <- countanalysis(counts = counts_consPeaks,
####                              pval = 0.01,
####                              lfcvalue = 1)
#### categaltre_peaks <- categAltrePeaks(altre_peaks,
####                                     lfctypespecific = 1.5,
####                                     lfcshared = 1.2,
####                                     pvaltypespecific = 0.01,
####                                     pvalshared = 0.05)
#### callGREAT <- runGREAT(peaks=categaltre_peaks)
####pathGREAT <- processPathways(callGREAT)
#### con <- "GREAToutput.zip"
#### writeGREATpathExcell(pathenrichOut = pathGREAT, con = con)
#### }
####
###
###writeGREATpathExcell <-
###  function(pathenrichOut, con) {
###
###    listNames <- names(pathenrichOut)
###    pathNames <- names(pathenrichOut[[1]]$Sig_Pathways)
###    fileExt <- tools::file_ext(con)
###    fileCon <- stringr::str_replace(con,
###                                    fileExt ,
###                                    stringr::str_c(listNames,
###                                                   "xlsx",
###                                                   sep = "."))
###
###    for(i in 1:length(pathNames)) {
###	xlsx::write.xlsx(pathenrichOut[[1]]$Sig_Pathways[[i]],file=fileCon[[1]],
###		sheetName=pathNames[i],append=TRUE,row.names=FALSE)
###    }
###
###    for(files in 2:length(fileCon)) {
###	for(i in 1:length(pathNames)) {
###        	xlsx::write.xlsx(pathenrichOut[[files]]$Sig_Pathways[[i]],
###		file=fileCon[[files]],
###                sheetName=pathNames[i],append=TRUE, row.names=FALSE)
###    	}
###    }
###
###    utils::zip(zipfile = con, files = fileCon)
###    #if(file.exists(con)) {
###    #  file.rename(paste0(con, ".zip"), con)}
###    #tar(pathenrichOut,fileCon)
###  }


#' Writes a data generated by pathenrich
#'
#' Creates  three CSV files containing the results of the pathway enrichment analysis for
#' experiment-specific, reference-specific, and shared REs.
#'
#' @param pathenrichOut output generated from pathenrich()
#' @param con connection filepath
#'
#' @examples
#' \dontrun{
#' csvfile <- file.path(dir="yourfilepath", 'sampleinfo.csv')
#' sampleinfo <- loadCSVFile(csvfile)
#' samplePeaks <- loadBedFiles(sampleinfo)
#' consPeaks <- getConsensusPeaks(samplepeaks=samplePeaks,minreps=2)
#' TSSannot <- getTSS()
#' consPeaksAnnotated <- combineAnnotatePeaks(conspeaks = consPeaks,
#'                                           TSS = TSSannot,
#'                                           merge = TRUE,
#'                                           regionspecific = TRUE,
#'                                           distancefromTSSdist = 1500,
#'                                           distancefromTSSprox = 1000)
#' #Need to run getcounts on all chromosomes
#' counts_consPeaks <- getcounts(annotpeaks = consPeaksAnnotated,
#'                              csvfile = csvfile,
#'                              reference = 'SAEC')
#'                              #' altre_peaks <- countanalysis(counts = counts_consPeaks,
#'                              pval = 0.01,
#'                              lfcvalue = 1)
#' categaltre_peaks <- categAltrePeaks(altre_peaks,
#'                                     lfctypespecific = 1.5,
#'                                     lfcshared = 1.2,
#'                                     pvaltypespecific = 0.01,
#'                                     pvalshared = 0.05)
#' callGREAT <- runGREAT(peaks=categaltre_peaks)
#' pathGREAT <- processPathways(callGREAT)
#' con <- "GREAToutput.zip"
#' writeGREATpath(pathenrichOut = pathGREAT, con = con)
#' }
#'
#' @export
writeGREATpath <- function(pathenrichOut, con) {

    listNames <- names(pathenrichOut)
    pathNames <- names(pathenrichOut[[1]]$Sig_Pathways)

    # fileNames <- as.character(unlist(lapply(listNames,
    #                                         function(x)
    #                                           paste(x,
    #                                                 pathNames,
    #                                                 sep = "_"))))

    fileExt <- tools::file_ext(con)

    allfiles = c()
    for (l in listNames) {
      for (p in pathNames) {
        fileCon <- stringr::str_replace(con,
                                        fileExt,
                                        stringr::str_c(paste(l, p, sep = "_"),
                                                       "csv", sep = "."))
        message(paste("Writing", fileCon))
        readr::write_csv(pathenrichOut[[l]]$Sig_Pathways[[p]], fileCon)
        allfiles <- c(allfiles, fileCon)
      }
    }
    utils::zip(zipfile = con, files = allfiles)
    #if(file.exists(con)) {
    #  file.rename(paste0(con, ".zip"), con)}
    #tar(pathenrichOut,fileCon)
  }

